package com.anjie.powerproject.controller;

import com.anjie.powerproject.common.Result;
import com.anjie.powerproject.entity.GraphChangeLog;
import com.anjie.powerproject.entity.GraphInfo;
import com.anjie.powerproject.pojo.dto.GraphChangeLogDTO;
import com.anjie.powerproject.repository.GraphInfoRepository;
import com.anjie.powerproject.service.*;
import com.anjie.powerproject.strategy.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RequestMapping("graphChangeLog")
@Controller
public class GraphChangeLogController {

    @Autowired
    private GraphChangeLogService graphChangeLogService;
    @Autowired
    private GraphInfoRepository graphInfoRepository;
    /**
     * 根据id获取图谱修改的信息，并跳转到查看界面
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/getInfo/{id}")
    private String getGraphChangeLogInfo(@PathVariable("id") Integer id, Model model){
        GraphChangeLog log = graphChangeLogService.getGraphChangeLogById(id);
        model.addAttribute("graphChangeLog",log);
        return "knowChartChangeInfo";
    }

    /**
     * 根据点位id获取图谱修改，并跳转到模型修改页面
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/update/{id}")
    private String updateGraphChangeLog(@PathVariable("id") Integer id,Model model){
        GraphChangeLog log = graphChangeLogService.getGraphChangeLogById(id);
        model.addAttribute("graphChangeLog",log);
        return "knowChartChangeEdit";
    }

    @RequestMapping("/getAll")
    @ResponseBody
//    Result result=new Result("dd",true,1,"11");

    public Result getAll(){
        List<GraphChangeLogDTO> graphChangeLogDTOS=graphChangeLogService.getAll();

        Result result=new Result();
        result.setData(graphChangeLogDTOS);
        result.setMsg("获取成功");
//        return new Result(graphChangelogs,true,1,"获取成功");
        return result;
    }

    @RequestMapping(value = "/deleteById")
    @ResponseBody
    public Result deleteById(@RequestParam(value = "id", required = false, defaultValue = "") Integer id){
        if(id==null){
            return new Result(null,false,0,"删除失败，Id为空");
        }
        graphChangeLogService.deleteById(id);
        return new Result(null,true,1,"删除成功");
    }

    @RequestMapping("/update")
    @ResponseBody
    public Result updateById(GraphChangeLog graphChangeLog){

        GraphChangeLog graphLog=graphChangeLogService.saveAndFlush(graphChangeLog);

        if (graphLog==null){

            return new Result(null,false,0,"修改失败");
        }
        return new Result(null,true,1,"修改成功");
    }

    @RequestMapping("/getGraphChangeLogById")
    @ResponseBody
    public Result getDataById(@RequestParam(value = "id", required = false, defaultValue = "") Integer id ){

        GraphChangeLog graphLog=graphChangeLogService.getGraphChangeLogById(id);

        if (graphLog==null){
            return new Result(null,false,0,"查询失败");
        }
        return new Result(graphLog,true,1,"查询成功");
    }


}
